from PyQt5.QtCore import Qt, QModelIndex
from PyQt5.QtWidgets import QStyledItemDelegate, QDoubleSpinBox


class Decimal(QStyledItemDelegate):
    def __init__(self, parent, constraints):
        super().__init__(parent)
        self.constraints = constraints


    def createEditor(self, parent, option, index):
        spin = QDoubleSpinBox(parent)
        spin.setFrame(False)

        return spin

        
    def setEditorData(self, spin, index):
        value = index.model().data(index, Qt.EditRole)
        if(value==''):
            spin.setValue(0)
        else:
            spin.setValue(float(value))



    def setModelData(self, spin, model, index):
        spin.interpretText()
        model.setData(index, spin.value(), Qt.EditRole)
        

    def updateEditorGeometry(self, spin, option, index):
        spin.setGeometry(option.rect)
